Imports System
Imports System.Collections.Specialized
Imports System.Security.Cryptography
Imports System.Text
Imports System.Web


''' <summary>
''' Quick MD5 crypt wrapper.
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' 	[cinj]	06/09/2004	Created
''' </history>
Friend Class SimpleCrypt
    Private m_buffer As String

    Public Sub New()
    End Sub 'New


    ''' <summary>
    ''' Uses default key for decryption
    ''' </summary>
    ''' <param name="encryptedString"></param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[cinj]	16/06/2006	Created
    ''' </history>

    Public Sub New(ByVal encryptedString As String)
        m_buffer = decrypt(HttpUtility.UrlDecode(encryptedString.Replace("_", "%")))
    End Sub 'New


    ''' <summary>
    ''' Overrides the default encryption key
    ''' </summary>
    ''' <param name="encryptedString"></param>
    ''' <param name="ckey"></param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[cinj]	16/06/2006	Created
    ''' </history>

    Public Sub New(ByVal encryptedString As String, ByVal ckey As String)
        Me.cryptoKey = ckey
        m_buffer = decrypt(HttpUtility.UrlDecode(encryptedString.Replace("_", "%")))
    End Sub 'New


    ''' <summary>
    ''' Return encrypted content
    ''' </summary>
    ''' <value></value>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[cinj]	16/06/2006	Created
    ''' </history>

    Public ReadOnly Property EncryptedString() As String
        Get
            Return System.Web.HttpUtility.UrlEncode(encrypt(Me.m_buffer)).Replace("%", "_")
        End Get
    End Property


    ''' <summary>
    ''' Return content unencrypted
    ''' </summary>
    ''' <value></value>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[cinj]	16/06/2006	Created
    ''' </history>

    Public Property OriginalString() As String
        Get
            Return m_buffer
        End Get
        Set(ByVal Value As String)
            m_buffer = Value
        End Set
    End Property


    ''' <summary>
    ''' Return encrypted content
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[cinj]	16/06/2006	Created
    ''' </history>

    Public Overrides Function ToString() As String
        Return Me.EncryptedString
    End Function 'ToString

    Private Function encrypt(ByVal serializedQueryString As String) As String
        Dim buffer As Byte() = Encoding.ASCII.GetBytes(serializedQueryString)
        Dim des As New TripleDESCryptoServiceProvider
        Dim MD5 As New MD5CryptoServiceProvider
        des.Key = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(cryptoKey))
        des.IV = IV
        Return Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length))
    End Function 'encrypt

    Private Function decrypt(ByVal encryptedQueryString As String) As String
        Dim buffer As Byte() = Convert.FromBase64String(encryptedQueryString)
        Dim des As New TripleDESCryptoServiceProvider
        Dim MD5 As New MD5CryptoServiceProvider
        des.Key = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(cryptoKey))
        des.IV = IV
        Try
            Return Encoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length))
        Catch ex As Exception
            Console.WriteLine(ex.ToString())
            Return ""
        End Try
    End Function 'decrypt

    Public WriteOnly Property Key() As String
        Set(ByVal Value As String)
            Me.cryptoKey = Value
        End Set
    End Property

    Private cryptoKey As String = "2117E22B6FDE428b8AA5B1305817F336CA06A5B1FE194972BF35AB909AE5D877"
    Private IV() As Byte = {240, 3, 45, 29, 0, 76, 173, 59}
End Class

